using Sustainalytics.ClientDW.Synchronization.SyncESGData.AbstractClassesAndInterfaces;
using Sustainalytics.SharedEntities.EsgResearch;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Sustainalytics.ClientDW.Synchronization.SyncESGData.RPIndicatorExtendedLogic
{
    public class RPIndicatorExtendedOrchestrator : ICoreEsgSyncOrchestrator<EsgResearchProfile>, IComprehensiveEsgSyncOrchestrator<EsgResearchProfile>
    {
        private readonly RPIndicatorExtendedMapper rpIndicatorExtendedMapper;
        private readonly RPIndicatorExtendedCommand rpIndicatorExtendedCommand;
        private readonly RPIndicatorExtendedProcessor rpIndicatorExtendedProcessor;

        public RPIndicatorExtendedOrchestrator(
            RPIndicatorExtendedMapper rpIndicatorExtendedMapper,
            RPIndicatorExtendedCommand rpIndicatorExtendedCommand,
            RPIndicatorExtendedProcessor rpIndicatorExtendedProcessor)
        {
            this.rpIndicatorExtendedMapper = rpIndicatorExtendedMapper;
            this.rpIndicatorExtendedCommand = rpIndicatorExtendedCommand;
            this.rpIndicatorExtendedProcessor = rpIndicatorExtendedProcessor;
        }

        async Task ICoreEsgSyncOrchestrator<EsgResearchProfile>.OrchestrateAsync(IList<EsgResearchProfile> coreEsgResearchProfiles, IList<Guid> invalidatedCompaniesIds)
        {
            await OrchestrateAsync(coreEsgResearchProfiles, invalidatedCompaniesIds);
        }

        async Task IComprehensiveEsgSyncOrchestrator<EsgResearchProfile>.OrchestrateAsync(IList<EsgResearchProfile> comprehensiveEsgResearchProfiles, IList<Guid> invalidatedCompaniesIds)
        {
            await OrchestrateAsync(comprehensiveEsgResearchProfiles, invalidatedCompaniesIds);
        }

        private async Task OrchestrateAsync(IList<EsgResearchProfile> esgResearchProfiles, IList<Guid> invalidatedCompaniesIds)
        {
            var indicators = rpIndicatorExtendedMapper.MapManyToMany(esgResearchProfiles);

            rpIndicatorExtendedProcessor.AddIndicatorInformation(indicators);

            await rpIndicatorExtendedCommand.ExecuteAsync(indicators, invalidatedCompaniesIds).ConfigureAwait(false);
        }
    }
}
